package com.itboye.ihomebank.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.home.ActivityRenZheng;
import com.itboye.ihomebank.home.ActivityXuanZuKe;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 发布
 * 
 */
public class MyFaBuAdapter extends MutiplyCommonAdapter<String> implements
		Observer {
	private Activity context;

	List<String> list = new ArrayList<String>();
	HousePresenter housePresenter;

	protected String uid;

	private String EVENT_TYPE_UNKNOWN = "请求错误";

	public MyFaBuAdapter(Activity context, List<String> datas, int... layoutId) {
		super(context, datas, layoutId);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.list = datas;
		housePresenter = new HousePresenter(this);
		uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
	}

	// public MyFaBuAdapter(Activity context, List<String> list) {
	// this.context = context;
	// this.list = list;
	// }
	//
	// @Override
	// public int getCount() {
	// return list.size();
	// }
	//
	// @Override
	// public Object getItem(int position) {
	// return list.get(position);
	// }
	//
	// @Override
	// public long getItemId(int position) {
	// return position;
	// }
	//
	// @Override
	// public View getView(int position, View view, ViewGroup parent) {
	// ViewHolder holder = null;
	// if (view == null) {
	// holder = new ViewHolder();
	// view = LayoutInflater.from(context).inflate(R.layout.item_wodefabu,
	// null);
	// holder.house_tag = (TextView) view.findViewById(R.id.house_tag);
	// holder.timg = (ImageView) view.findViewById(R.id.img_top);
	// holder.house_name = (TextView) view.findViewById(R.id.house_name);
	// holder.xuanzuke = (TextView) view.findViewById(R.id.xuanzuke);
	// holder.house_price = (TextView) view.findViewById(R.id.house_price);
	// holder.zhiding = (TextView) view.findViewById(R.id.zhiding);
	// holder.shuaxin = (TextView) view.findViewById(R.id.shuaxin);
	// holder.renzheng = (TextView) view.findViewById(R.id.renzheng);
	// holder.xiajia = (TextView) view.findViewById(R.id.xiajia);
	// holder.addr = (TextView) view.findViewById(R.id.addrdetail);
	// holder.zukenum = (TextView) view.findViewById(R.id.zukenum);
	// view.setTag(holder);
	// } else {
	// holder = (ViewHolder) view.getTag();
	// }
	// holder.house_tag.setText(list.get(position).get("zt").toString());
	// holder.timg.setBackgroundResource(Integer.valueOf(list.get(position)
	// .get("img").toString()));
	// holder.house_name.setText(list.get(position).get("title").toString());
	// holder.house_price.setText(list.get(position).get("money").toString());
	// holder.renzheng.setOnClickListener(new OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	// Intent intent=new Intent(context,ActivityRenZheng.class);
	// context.startActivity(intent);
	// }
	// });
	// holder.xuanzuke.setOnClickListener(new OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	// Intent intent=new Intent(context,ActivityXuanZuKe.class);
	// context.startActivity(intent);
	// }
	// });
	// return view;
	// }

	// class ViewHolder {
	// TextView house_tag;
	// ImageView timg;
	// TextView house_name;
	// TextView house_price;
	// TextView addr;
	// TextView zhiding, shuaxin, renzheng, xiajia, xuanzuke;
	// TextView zukenum;
	// }

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void convert(ViewHolder holder, final String t, int type,
			int position) {
		// TODO Auto-generated method stub
		holder.setOnClickListener(R.id.xuanzuke, position,// 选租客
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(context,
								ActivityXuanZuKe.class);
						context.startActivity(intent);
					}
				});
		holder.setOnClickListener(R.id.xiajia, position, new OnClickListener() {// 下架

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						housePresenter.xiajia(uid, t);// 下架接口
					}
				});
		holder.setOnClickListener(R.id.renzheng, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(context,
								ActivityRenZheng.class);
						context.startActivity(intent);
					}
				});
		holder.setOnClickListener(R.id.shuaxin, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// 刷新
					}
				});

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {

		}
	}

	public ResultEntity handlerError(Object data) {
		ResultEntity result = (ResultEntity) data;
		if (result == null) {
			result = new ResultEntity("-1", "数据格式错误!~", data);
			return result;
		}

		if (result.hasError()) {
			ByAlert.alert(result.getMsg());
			result.setEventType(EVENT_TYPE_UNKNOWN);
			return result;
		}

		return result;
	}

}
